import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core'
import { DetailTabClass } from '@shared/classes/DetailTabClass'
import { TradeInfo, Acquirer, IUpdateAcquirerTradeInfo, MerchantFeature, IUpdateAcquirerFeature } from '@shared/interfaces/acquirer'
import { PageService } from '@shared/services/page.service'
import { ActivatedRoute, Router } from '@angular/router'
import { CommonAcquirerService } from '@shared/services/acquirer.service'
import { AcquirerService } from '../../acquirer.service'
import { NzMessageService } from 'ng-zorro-antd'
import { I18NService } from '@core'

interface DualMessageData {
  isSupportDualMessage: boolean
}

/**
 * 机构功能配置
 * 1. 单双信息
 * 2. 审批功能
 * 3. 风控功能
 */
@Component({
  selector: 'app-acquirer-detail-tab-func-acquirer',
  templateUrl: './tab-func-acquirer.component.html',
})
export class AcquirerDetailTabFuncAcquirerComponent extends DetailTabClass<TradeInfo> implements OnInit {

  @ViewChild('tpIsSupportDualMessage') tpIsSupportDualMessage: TemplateRef<void>

  @ViewChild('dualOperate') dualOperate: TemplateRef<void>

  fields = {
    DualMessage: '1' // 双信息
  }

  /** 版本号 */
  version = 0

  /** 单双信息 */
  feature: MerchantFeature

  constructor(
    public pageService: PageService,
    public route: ActivatedRoute,
    private router: Router,
    private commonAcquirerService: CommonAcquirerService,
    private acquirerService: AcquirerService,
    private messageService: NzMessageService,
    private i18n: I18NService,
  ) {
    super(pageService, route)
  }

  ngOnInit() {
    this.initialData()
  }

  initialData(): void {
    this.commonAcquirerService.viewAcquirer$.value$.subscribe(acquirer => {
      this.initialColumns()
      this.version = acquirer.institution.version

      /** 渲染单双信息功能配置 */
      this.renderDualMessageConfig(acquirer)

    })
  }

  onCancel() {}

  onSubmit() {
    try {
      if (this.field === this.fields.DualMessage) {
        /** 单双信息 */
        this.submitDualMessage()
      }
    } catch (e) {
      console.error(e)
    }
  }

  submitDualMessage(): void {
    const data: IUpdateAcquirerFeature = {
      version: this.version,
      feature: this.feature
    }
    this.acquirerService.updateFeaturesOfAcquirer(data, this.getID()).subscribe(acquirer => {
      this.commonAcquirerService.setViewingAcquirer(acquirer)
      this.initialData()
    })
  }

  renderDualMessageConfig(acquirer: Acquirer) {
    this.feature = acquirer.institution.common.feature
    const { isSupportDualMessage } = this.feature
    const dualMessageData = {
      isSupportDualMessage
    }
    this.setEdit(this.fields.DualMessage, !!dualMessageData ? Object.keys(dualMessageData).length : 0)
    this.setupDualMessageColumns(dualMessageData)
  }

  setupDualMessageColumns(dualMessageData: DualMessageData): void {
    const { isSupportDualMessage } = dualMessageData
    this.columns[this.fields.DualMessage] = [
      [
        {
          span: 24,
          label: this.labelFeature(),
          value: this.parseBoolToString(dualMessageData.isSupportDualMessage),
          displayValue: this.renderSingleOrDual(isSupportDualMessage),
          template: this.tpIsSupportDualMessage,
        },
        /** 屏蔽对机构单双信息模式修改的操作 */
        // {
        //   span: 4,
        //   label: 'GENERAL.INFO.OPERATION',
        //   labelI18n: 'GENERAL.INFO.OPERATION',
        //   template: this.dualOperate,
        //   operate: true,
        //   acl: ['api.put.camel.institution.one.trade'],
        // }
      ]
    ]
  }

  initialColumns(): void {
    for (const key in this.fields) {
      this.columns[this.fields[key]] = []
    }
  }

  switchIsSupportDualMessage(value: string): void {
    this.feature.isSupportDualMessage = this.parseStringToBool(value)
  }

  private renderSingleOrDual(value: boolean): string {
    return value ? this.trans('BIZ.ACQUIRER.ACQUIRER_FUNC.DUAL_MESSAGE') : this.trans('BIZ.ACQUIRER.ACQUIRER_FUNC.SINGLE_MESSAGE')
  }

  private trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }

  parseBoolToString(isSupportDualMessage: boolean): string {
    return !!isSupportDualMessage ? '1' : '0'
  }

  parseStringToBool(str: string): boolean {
    return !!(+str)
  }

  labelFeature(): string {
    return this.trans('BIZ.ACQUIRER.ACQUIRER_FUNC.SINGLE_MESSAGE') + '/' + this.trans('BIZ.ACQUIRER.ACQUIRER_FUNC.DUAL_MESSAGE')
  }
}
